package org.millioncall.yueyoga.admin.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.Status;
import org.millioncall.yueyoga.admin.service.StatusService;
import org.millioncall.yueyoga.common.utils.ZhuwWebAppUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

//--created by lisj on 2014.03.06
@Controller
public class StatusAction extends BaseAction {
	private StatusService statusService;

	public StatusService getStatusService() {
		return statusService;
	}

	public void setStatusService(StatusService statusService) {
		this.statusService = statusService;
	}

	@RequestMapping(value = "/substatus.jhtml")
	public void findSubstatus(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {

		super.initFrontHeader(request, model);

		String msg = "";
		String code = request.getParameter("code");// 获得父类编码
		List<Status> statuses = statusService.findByParentCode(code);// 根据父类编码查询子状态
		if(statuses.size()>0) {
			StringBuffer sb = new StringBuffer("[");
			for (Status status : statuses) {
				sb.append("{");
				sb.append("'id':'" + status.getId() + "',");
				sb.append("'name':'" + status.getName() + "',");
				sb.append("'code':'" + status.getCode() + "',");
				sb.append("'type':'" + status.getType() + "',");
				sb.append("'parentCode':'" + status.getParentcode() + "'},");
			}
			String str = sb.toString();
			str = str.substring(0, str.length() - 1);
			str += "]";
			msg = "{'success':true, 'items':" + str + "}";
		} else {
			msg = "{'success':false, 'msg':'子渠道未找到数据！'}";
		}
		
		ZhuwWebAppUtil.ajaxResponse(response, msg);
	}

	@RequestMapping(value = "/findStatusBytype.jhtml")
	public void findStatusBytype(Integer type, HttpServletRequest request,
			HttpServletResponse response) {
		String msg = "";
		List<Status> statuses = statusService.findByType(type);
		if(statuses.size()>0) {
			StringBuffer sb = new StringBuffer("[");
			for (Status status : statuses) {
				sb.append("{");
				sb.append("'id':'" + status.getId() + "',");
				sb.append("'name':'" + status.getName() + "',");
				sb.append("'code':'" + status.getCode() + "',");
				sb.append("'type':'" + status.getType() + "',");
				sb.append("'parentCode':'" + status.getParentcode() + "'},");
			}
			String str = sb.toString();
			str = str.substring(0, str.length() - 1);
			str += "]";
			msg = "{'success':true, 'items':" + str + "}";
		} else {
			msg = "{'success':false, 'msg':'该栏目没有一级状态！'}";
		}
		ZhuwWebAppUtil.ajaxResponse(response, msg);
	}

	@RequestMapping(value = "/findStatusList.jhtml")
	public String toStatus(ModelMap context, HttpServletRequest request,
			HttpServletResponse response) {
		super.initFrontHeader(request, context);

		int pageNo = 1;
		int pageSize = 20;

		String strStart = request.getParameter("pageNo");// 获得页码数
		if (strStart != null) {
			pageNo = Integer.parseInt(strStart);
		}
		String strLimit = request.getParameter("limit");
		if (strLimit != null) {
			pageSize = Integer.parseInt(strLimit);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		PaginatedList<Status> p = statusService.getStatusInPage((pageNo - 1)
				* pageSize, pageSize, map);
		context.put("statusList", p);
		List<Status> statuses = statusService.findByType(1);
		context.put("statuses", statuses);
		return "status_search";
	}

	// 进入状态添加
	@RequestMapping(value = "/toAddStatus.jhtml")
	public String toAddStatus(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {

		super.initFrontHeader(request, model);

		List<Status> statuses = statusService.findByType(1);
		model.put("statuses", statuses);
		return "status_add";
	}

	// 添加状态
	@RequestMapping(value = "/addStatus.jhtml")
	public String addStatus(ModelMap context, String name, String code,
			String type, String parentcode, HttpServletRequest request,
			HttpServletResponse response) {
		Status status = new Status();
		status.setName(name);
		status.setCode(code);
		status.setType(Integer.parseInt(type));
		if (parentcode != null && !parentcode.equals("")) {
			status.setParentcode(parentcode);
		} else {
			status.setParentcode(null);
		}
		if (statusService.insert(status)) {
			context.put("msg", "状态添加成功!");
		} else {
			context.put("msg", "状态添加失败!");
		}
		return this.toStatus(context, request, response);
	}

	// 进入状态修改
	@RequestMapping(value = "/toEditStatus.jhtml")
	public String toEditStatus(ModelMap context, Integer id,
			HttpServletRequest request, HttpServletResponse response) {
		
		super.initFrontHeader(request, context);
		
		Status status = statusService.findById(id);
		context.put("statu", status);
		List<Status> statuses = statusService.findByType(status.getType());
		context.put("statuses", statuses);
		Status parentStatus = new Status();
		if (status.getParentcode() == null) {
			parentStatus.setCode("");
			parentStatus.setName("顶级状态");
		} else {
			parentStatus = statusService.findByCode(status.getParentcode());
		}
		context.put("parentStatus", parentStatus);
		return "status_edit";
	}

	// 删除状态
	@RequestMapping(value = "/deleteStatus.jhtml")
	public void deletemember(HttpServletRequest request,
			HttpServletResponse response) {
		String msg = null;
		String ids = request.getParameter("ids");
		if (StringUtils.isBlank(ids)) {
			msg = "{'success':false,'msg':'未提交待删除状态的编号'}";
		} else {
			String[] arrids = ids.split(",");
			Integer[] deids = new Integer[20];
			for (int i = 0; i < arrids.length; i++) {
				if (arrids[i] != null && !arrids[i].equals("")) {
					deids[i] = Integer.parseInt(arrids[i]);
				}
			}
			try {
				statusService.deleteStatusByIds(deids);
				msg = "true";
			} catch (RuntimeException re) {
				msg = "false";
			}
		}
		WebAppUtils.ajaxResponse(response, msg);
	}

	// 修改状态
	@RequestMapping(value = "/editStatus.jhtml.jhtml")
	public String editStatus(ModelMap context, String statusCode, String name,
			String code, String type, String parentcode,
			HttpServletRequest request, HttpServletResponse response) {
		Status status = statusService.findByCode(statusCode);
		status.setName(name);
		status.setCode(code);
		status.setType(Integer.parseInt(type));
		if (parentcode != null && !parentcode.equals("")) {
			status.setParentcode(parentcode);
		} else {
			status.setParentcode(null);
		}
		if (statusService.update(status)) {
			context.put("msg", "状态添加成功!");
		} else {
			context.put("msg", "状态添加失败!");
		}
		return this.toStatus(context, request, response);
	}
}
